package org.itboys.mobile.controller.mobile.product;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.dto.product.SundayProductDto;
import org.itboys.mobile.entity.mysql.product.SundayProduct;
import org.itboys.mobile.entity.mysql.product.SundayProductCategory;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductCategoryService;
import org.itboys.mobile.service.mysql.mobile.product.SundayMobileProductService;
import org.itboys.mobile.util.AccountIdHoldUtils;
import org.itboys.mobile.util.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017/12/1
 * 描述：迈迪龙_手机端_产品
 */
@Controller
@RequestMapping("/sunday/mobile/product")
@Api(value = "product",description = "产品")
public class SundayMobileProductController extends BaseController{
    @Autowired
    private SundayMobileProductService productService;
    @Autowired
    private SundayMobileProductCategoryService categoryService;

    /**
     * 分页查询商品
     * @param request
     * @param response
     */
    @RequestMapping(value="/select", method= RequestMethod.POST)
    @ApiOperation(value="获取商品列表。分页")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = false, name = "name", value = "商品名称。模糊查询参数") @RequestParam(value = "name", required = false) String name,
            @ApiParam(required = false, name = "categoryId", value = "分类ID") @RequestParam(value = "categoryId", required = false) Long categoryId,
            @ApiParam(required = false, name = "type", value = "商品类型。1主机。2附件") @RequestParam(value = "type", required = false) Integer type,
            @ApiParam(required = false, name = "isIndexRecommend", value = "是否主页推荐  1是  否则不传") @RequestParam(value = "isIndexRecommend", required = false) Integer isIndexRecommend,
            @ApiParam(required = true, name = "pageSize", value = "分页参数。每页数量。默认20") @RequestParam(value = "pageSize", required = true) String pageSize,
            @ApiParam(required = true, name = "pageNumber", value = "分页参数。第几页。默认1") @RequestParam(value = "pageNumber", required = true) String pageNumber,
            @ApiParam(required = false, name = "sort", value = "排序参数。字段名称。默认为空。销量：saleNumber,价格：price") @RequestParam(value = "sort", required = false) String sort,
            @ApiParam(required = false, name = "order", value = "排序参数。默认为空。ASC正序/DESC倒序") @RequestParam(value = "order", required = false) String order,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            List<SundayProductDto> result=productService.selectProduct(request,memberId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/product/select----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

    /**
     * 移动端获取-产品-详情
     * @param request
     * @param response
     * @param productId
     * @param nonce
     * @param sign
     */
    @RequestMapping(value = "/findOne", method = RequestMethod.POST)
    @ApiOperation(value = "获取商品详情。code!=0直接提示消息")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(required = true, name = "productId", value = "商品ID") @RequestParam(value = "productId", required = true) Long productId,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign) {
        try{
            Long memberId = AccountIdHoldUtils.getMemberId();
            SundayProductDto result = productService.findOne(memberId,productId);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg =CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/product/findOne----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }
    /**
     * 查询商品类表
     * @param request
     * @param response
     */
    @RequestMapping(value="/category/select", method= RequestMethod.POST)
    @ApiOperation(value="获取商品分类列表。")
    public void selectCategory(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "随机数")@RequestParam(value = "nonce", required = true) String nonce,
            @ApiParam(value = "签名")@RequestParam(value = "sign", required = true) String sign){
        try{
            List<SundayProductCategory> result=productService.selectCategory(request);
            ResponseMessage.success(result);
        }catch (Exception e){
            String msg = CommonConstants.ERROMSG;
            Integer code=CommonConstants.FAIL_CODE;
            //自定义异常（签名错误）
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
                //token已过期
            }else  if(e.getClass().getSimpleName().equalsIgnoreCase("TokenException")){
                code=CommonConstants.TOKEN_CODE;
                msg=CommonConstants.TOKENMSG;
            }else{
                logger.error("/sunday/mobile/product/category/select----e="+e.getMessage());
            }
            e.printStackTrace();
            ResponseMessage.error(code,msg);
        }
    }

}
